#! /bin/sh -e

# All lines beginning with `# DP:' are a description of the patch.
# DP: Description: Fix glibc nscd client code to avoid triggering SIGPIPE
# DP: Patch author: Mike Mammarella <mdm@google.com>
# DP: Upstream status: Not submitted
# DP: Date: Sep 18 2006


if [ $# -ne 2 ]; then
    echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
    exit 1
fi
case "$1" in
    -patch) patch -d "$2" -f --no-backup-if-mismatch -p0 < $0;;
    -unpatch) patch -d "$2" -f --no-backup-if-mismatch -R -p0 < $0;;
    *)
	echo >&2 "`basename $0`: script expects -patch|-unpatch as argument"
	exit 1
esac
exit 0

--- nscd/nscd_helper.c	2006-09-14 19:39:08.504447000 -0700
+++ nscd/nscd_helper.c	2006-09-18 15:26:14.147144000 -0700
@@ -114,6 +114,24 @@
 }
 
 
+static ssize_t
+__writev_nosignal (int fd, const struct iovec *vector, int count)
+{
+  struct msghdr msg;
+  msg.msg_name = NULL;
+  msg.msg_namelen = 0;
+  msg.msg_iov = (struct iovec *) vector;
+  msg.msg_iovlen = count;
+  msg.msg_control = NULL;
+  msg.msg_controllen = 0;
+  msg.msg_flags = 0;
+#ifndef MSG_NOSIGNAL
+# define MSG_NOSIGNAL 0
+#endif
+  return __sendmsg (fd, &msg, MSG_NOSIGNAL);
+}
+
+
 static int
 open_socket (void)
 {
@@ -218,7 +236,7 @@
   iov[1].iov_base = (void *) key;
   iov[1].iov_len = keylen;
 
-  if (__builtin_expect (TEMP_FAILURE_RETRY (__writev (sock, iov, 2))
+  if (__builtin_expect (TEMP_FAILURE_RETRY (__writev_nosignal (sock, iov, 2))
 			!= iov[0].iov_len + iov[1].iov_len, 0))
     /* We cannot even write the request.  */
     goto out_close2;
@@ -445,7 +463,7 @@
       vec[1].iov_base = (void *) key;
       vec[1].iov_len = keylen;
 
-      ssize_t nbytes = TEMP_FAILURE_RETRY (__writev (sock, vec, 2));
+      ssize_t nbytes = TEMP_FAILURE_RETRY (__writev_nosignal (sock, vec, 2));
       if (nbytes == (ssize_t) (sizeof (request_header) + keylen)
 	  /* Wait for data.  */
 	  && wait_on_socket (sock) > 0)
